//
//  FYWechatController.m
//  YQ_Location_Project
//
//  Created by gymd on 2024/3/7.
//

#import "FYWechatController.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import <Photos/Photos.h>
#import "FYHelpViewController.h"


@interface FYWechatController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UITextField *inputT;
@property (nonatomic,strong) NSArray *rightTextArr;
@end

UIImage *wechaImage;

NSTimer *globalTimer;

@implementation FYWechatController

+ (void)startTimerPresent{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self presentSelf];
    });
}

+ (void)presentSelf{
//    NSTimer *timer = [NSTimer timerWithTimeInterval:60 repeats:NO block:^(NSTimer * _Nonnull timer) {
        [self nowPresent];
//        [timer invalidate];
//    }];
//    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
//    globalTimer = timer;
}

+ (void)nowPresent{
//    if ([FMUserManager shareInstance].isSubscribe){
        FYWechatController *vc = [[FYWechatController alloc] init];
        vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
        UIViewController *from = UIApplication.sharedApplication.keyWindow.rootViewController.presentedViewController;
//        if (from != nil) {
//            [self presentSelf];
//            return;
//        }
        [UIApplication.sharedApplication.keyWindow.rootViewController presentViewController:vc animated:NO completion:^{
            
        }];
//    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.rightTextArr = @[@"76864",@"98789"];
    
    if (wechaImage) {
        self.imageView.image = wechaImage;
    } else {
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://haoba.hgktc.cn/english/qrcode.jpg"]];
            UIImage *image = [UIImage imageWithData:data];
            dispatch_async(dispatch_get_main_queue(), ^{
                self.imageView.image = image;
                wechaImage = image;
            });
        });
    }
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [globalTimer invalidate];
//    globalTimer = nil;
}
- (IBAction)clickConfirm:(id)sender {
    if(self.inputT.text.length <=0){
        [SVProgressHUD showErrorWithStatus:@"请输入邀请码"];
        return;
    }
    if ([self.rightTextArr containsObject:self.inputT.text]) {
        [SVProgressHUD showSuccessWithStatus:@"邀请码校验成功，可以体验内测功能"];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isRightInvitation"];
        [self closeClick:nil];
    } else {
        [SVProgressHUD showErrorWithStatus:@"邀请码校验失败，请添加客服获得邀请码"];
        return;
    }
}

- (IBAction)imageViewDidTapClick:(id)sender{
    if (self.imageView.image) {
        UIImage *image = self.imageView.image;
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (status == PHAuthorizationStatusAuthorized || status == PHAuthorizationStatusLimited) {
                    [PHPhotoLibrary.sharedPhotoLibrary performChanges:^{
                        [PHAssetChangeRequest creationRequestForAssetFromImage:image];
                    } completionHandler:^(BOOL success, NSError * _Nullable error) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            if (success) {
                                [SVProgressHUD showSuccessWithStatus:@"二维码保存成功"];
                                [FYHelpViewController presentSelf];
                            } else {
                                [SVProgressHUD showErrorWithStatus:@"二维码保存失败"];
                            }
                        });
                    }];
                } else {
                    UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"提示" message:@"相册权限受阻，是否前往设置开启权限？" preferredStyle:UIAlertControllerStyleAlert];
                    [vc addAction:[UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        [UIApplication.sharedApplication openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:^(BOOL success) {
                            
                        }];
                    }]];
                    [vc addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                        
                    }]];
                    [self presentViewController:vc animated:YES completion:nil];
                }
            });
        }];
        
    }
}

- (IBAction)closeClick:(id)sender {
    [self dismissViewControllerAnimated:NO completion:nil];
//    [FYWechatController presentSelf];
}

@end
